﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
using System.Data;

namespace PerCiGal.DAL
{
	public class SqlceHelper
    {
        #region SqlCeParameter Reader

        public static SqlCeParameter ParamString(string name, object value)
        {
            SqlCeParameter result = new SqlCeParameter();

            result.ParameterName = name;
            result.Value = value;

            if (string.IsNullOrEmpty((string)value))
            {
                result.Value = DBNull.Value;
            }

            return result;
        }

        public static SqlCeParameter ParamObj(string name, object value)
        {
            SqlCeParameter result = new SqlCeParameter();

            result.ParameterName = name;

            if (value == null)
            {
                result.Value = DBNull.Value;
            }
            else
            {
                result.Value = value;
            }

            return result;
        }

        public static SqlCeParameter ParamDateTime(string name, object value)
        {
            SqlCeParameter result = new SqlCeParameter();

            result.ParameterName = name;
            result.DbType = DbType.DateTime;
            result.Value = value;

            return result;
        }

        #endregion

        #region SqlCeParameter Insert

        public static string DataReaderString(SqlCeDataReader myDataReader, string name)
        {
            if (myDataReader[name] != DBNull.Value)
            {
                return myDataReader[name].ToString();
            }
            return string.Empty;
        }

        public static System.Nullable<DateTime> DataReaderDateTimeNullable(SqlCeDataReader myDataReader, string name)
		{
			if (myDataReader[name] != DBNull.Value)
			{
				return (DateTime)myDataReader[name];
			}
			return null;
		}

        public static DateTime DataReaderDateTime(SqlCeDataReader myDataReader, string name)
        {
            if (myDataReader[name] != DBNull.Value)
            {
                return (DateTime)myDataReader[name];
            }
            else throw new ArgumentNullException(name);
        }

        public static System.Nullable<int> DataReaderIntNullable(SqlCeDataReader myDataReader, string name)
		{
			if (myDataReader[name] != DBNull.Value)
			{
				return (int)myDataReader[name];
			}
			return null;
		}

        public static int DataReaderInt(SqlCeDataReader myDataReader, string name)
        {
            if (myDataReader[name] != DBNull.Value)
            {
                return (int)myDataReader[name];
            }
            else throw new ArgumentNullException(name);
        }

        public static System.Nullable<short> DataReaderShortNullable(SqlCeDataReader myDataReader, string name)
        {
            if (myDataReader[name] != DBNull.Value)
            {
                return (short)myDataReader[name];
            }
            return null;
        }

        public static System.Nullable<bool> DataReaderBoolean(SqlCeDataReader myDataReader, string name)
		{
			if (myDataReader[name] != DBNull.Value)
			{
				return (bool)myDataReader[name];
			}
			return null;
        }

        public static object DataReaderObject(SqlCeDataReader myDataReader, string name)
        {
            if (myDataReader[name] != DBNull.Value)
            {
                return myDataReader[name];
            }
            return null;
        }

        #endregion
    }
}
